﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using MvcEngine.Core;
using MvcEngine.Core.Services;
using MvcEngine.Mvc.Extensions;
using MvcEngine.Mvc.Constraints;
using MvcEngine.Core.Content;
using MvcEngine.Core.HttpHandlers;
using System.Web.Routing;

namespace MvcEngine.Mvc.Areas.StarterSite
{
    public class StarterSiteRoutes : AreaRegistration
    {
        public override void RegisterArea(AreaRegistrationContext context)
        {
            RegisterRoutes(context);
        }

        public override string AreaName
        {
            get { return "StarterSite"; }
        }

        public virtual void RegisterRoutes(AreaRegistrationContext context)
        {
            //context.MapMvcEngineRoute(
            //     "StarterSite_ModuleCssFile",
            //     "StarterSite/Module/{moduleName}/Css/{fileName}",
            //     new string[] { "MvcEngine.Mvc.StarterSite.Controllers" },
            //     new CssHandler()
            //);

            RegisterControlRoutes(context);

            RegisterUserRoutes(context);
            RegisterErrorRoutes(context);
            RegisterDefaultRoutes(context);
        }

        protected virtual void RegisterControlRoutes(AreaRegistrationContext context)
        {
            var modules = ContentManager.Current.GetModules();
            foreach (var item in modules)
            {
                var controls = ContentManager.Current.GetControls(item.Id);
                foreach (var control in controls)
                {
                    if (!string.IsNullOrEmpty(control.ControlServiceType))
                    {
                        Type type = Type.GetType(control.ControlServiceType);
                        IControlService service = (IControlService)IoC.Resolve(type);
                        var controlRoutes = new RouteCollection();
                        service.RegisterStarterSiteRoutes(new AreaRegistrationContext("StarterSite", controlRoutes, null));
                        foreach (var routeBase in controlRoutes)
                        {
                            var route = routeBase as Route;
                            if (route != null)
                            {
                                route.Defaults.Add("controlKey", control.ControlKey);
                                context.Routes.Add((string)route.DataTokens["RouteName"], route);
                            }
                        }
                    }
                }
            }
        }

        protected virtual void RegisterErrorRoutes(AreaRegistrationContext context)
        {
            //context.MapMvcEngineRoute(
            //    "BackOffice_Error",
            //    "Error",
            //    new { controller = "Error", action = "Index" },
            //    new string[] { "MvcEngine.Mvc.BackOffice.Controllers" } 
            //);
        }

        protected virtual void RegisterUserRoutes(AreaRegistrationContext context)
        {

            //context.MapMvcEngineRoute(
            //    "BackOffice_LanguageGet",
            //    "Language/{language}",
            //    new { controller = "User", action = "Language" },
            //    new string[] { "MvcEngine.Mvc.BackOffice.Controllers" } 
            //);

            //context.MapMvcEngineRoute(
            //    "BackOffice_LanguagePost",
            //    "Language",
            //    new { controller = "User", action = "Language" },
            //    new string[] { "MvcEngine.Mvc.BackOffice.Controllers" } 
            //);
        }

        protected virtual void RegisterDefaultRoutes(AreaRegistrationContext context)
        {
            context.MapMvcEngineRoute(
                "StarterSite_Page",
                "{pageName}",
                new { controller = "Home", action = "Index", pageName = "" },
                new string[] { "MvcEngine.Mvc.StarterSite.Controllers" }
            );

            context.MapMvcEngineRoute(
                "All",
                "{*all}",
                new { controller = "Error", action = "Error", viewName = "PageNotFound" },
                new string[] { "MvcEngine.Mvc.StarterSite.Controllers" }
            );
        }
    }
}
